library(ggplot2)

#Generates ggplot2 default colors
gg_color_hue <- function(n) {
  hues = seq(15, 375, length = n + 1)
  hcl(h = hues, l = 65, c = 100)[1:n]
}

paneld <- read.csv("racedosage.csv",header=T, stringsAsFactors=FALSE)
paneld$pe <- paneld$c1
paneld$se <- paneld$c2
paneld$lb <- paneld$pe- 1.96*paneld$se
paneld$ub <- paneld$pe+ 1.96*paneld$se

#Subgroup Factors
paneld$Population <- factor(paneld$c3,levels=c(2,1),
                            labels=c("Majority of Students","Minority of Students")[1:2] )

paneld$group <- factor(paneld$c4,levels=c(2,1),
                       labels=c("Feel Close to Blacks","Feel Close to Hispanics")[2:1])

#f1 = ggplot()
f1 = ggplot(data=paneld, mapping=aes(x=group, y=pe, ymin=lb, ymax=ub, width= 0.5, shape=Population, color= Population)) + geom_pointrange(size= 0.5, position = position_dodge(width=.5))
f1 = f1 + scale_shape_manual(name="Population",values=c(5,6), labels=paneld$Population)  
f1 = f1 + scale_color_manual(name="Population",values=gg_color_hue(2), labels=paneld$Population)
f1 = f1 + scale_fill_manual(values=c("black","black"))

#Error Bars
f1 = f1+ geom_errorbar(aes(ymin= paneld$pe - paneld$se, ymax= paneld$pe + paneld$se), width=.1, position=position_dodge(.5))

f1 = f1 + coord_flip()
f1 = f1 + geom_hline(yintercept=0, linetype="longdash", size=0.5)
f1 = f1 + labs( y= "Treatment Effect", x = "Dependent Variable", color = "Category")

#Reverse Legend
f1= f1 + guides(colour = guide_legend(reverse=TRUE),shape = guide_legend(reverse=TRUE))

#Limits - Set this to whatever you'd like limits to be.
#f1 = f1 + scale_y_continuous(limits = c(-0.2,0.2))

#Theme
f1 = f1+ theme(plot.background= element_rect(fill="grey100"),
              panel.background= element_rect(fill="grey100", color= "grey75"),
              panel.grid.major = element_line(color= "grey75"),
              panel.grid.minor = element_line(color= "grey80"),
              panel.grid.major.y = element_blank(),
              panel.grid.minor.y = element_blank(),
              panel.spacing.x = unit(12,"point"),
              axis.text = element_text(lineheight=0.5),
              legend.background = element_rect(fill="grey100")
              )
  
print(f1)